<?php
namespace Library;
use Think\Log;

/**
 * device管理类
 *
 * @author pufan
 */
class Beacon {
    private static $_instance;
    protected $region;

    private function __construct() {
        $this->region = D("region");
    }

    public function __clone() {
        trigger_error('Clone is not allow!', E_USER_ERROR);
    }

    public static function getInstance(){
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /*
     * 模拟用户摇一摇
     * type 1 为模拟全国device, 2为北京 device
     */
    function moniShake($type = 1) {
        $wxevent = new \Library\WxEvent();
        S('shake_event_ts:'.$_SESSION['open_id'], NULL);

        $moniShake = S('moniShake'.$_SESSION['open_id']);
        if($moniShake == false) {
            $moniShake = 1;
            S('moniShake'.$_SESSION['open_id'], 1, 3600*24);
        }
        Log::record('moniShake:'.$moniShake);

        if ($moniShake == 1) {
            $event = '{"ToUserName":"gh_e52449c80414","FromUserName":"'.$_SESSION['open_id'].'","CreateTime":"1465813343","MsgType":"event","Event":"ShakearoundUserShake","ChosenBeacon":{"Uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825","Major":"10060","Minor":"15179","Distance":"4.3305555145975712","Rssi":"-68","MeasurePower":"-68"},"ChosenPageId":"3133581"}';
            $wxevent->handle_shakearoundusershake(json_decode($event, TRUE));
            S('moniShake'.$_SESSION['open_id'], 2, 3600*24);

            Log::record('全国');
        } else {
            $event = '{"ToUserName":"gh_e52449c80414","FromUserName":"'.$_SESSION['open_id'].'","CreateTime":"1465813343","MsgType":"event","Event":"ShakearoundUserShake","ChosenBeacon":{"Uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825","Major":"10060","Minor":"15184","Distance":"4.3305555145975712","Rssi":"-68","MeasurePower":"-68"},"ChosenPageId":"3133581"}';
            $wxevent->handle_shakearoundusershake(json_decode($event, TRUE));
            S('moniShake'.$_SESSION['open_id'], 1, 3600*24);

            Log::record('北京');
        }
    }
}
